import time

import pytest
from appium.webdriver.common.mobileby import MobileBy
from selenium.common.exceptions import NoSuchElementException

from APP_UI_TEST.contacts_podemo.base.base_page import BasePage
from APP_UI_TEST.contacts_podemo.page.member_info_page import MemberInfoPage


class SearchMemberPage(BasePage):
    """ 搜索成员页 """

    def search_member(self,name):
        """搜索联系人 """
        # 在搜索框中输入联系人姓名
        self.driver.find_element(MobileBy.XPATH, "//*[@text='搜索']").send_keys(name)
        try:
            # 获取搜索结果第一条记录
            self.first_ele = self.driver.find_element(MobileBy.XPATH, "//*[@text='联系人']/../following-sibling::*[1]")
            return self
        except NoSuchElementException:
            if "无搜索结果" in self.driver.page_source:
                pytest.xfail(f"无搜索结果:{name}")

    def get_search_count(self):
        """ 获取搜索条目数"""
        time.sleep(2)
        self.search_count = len(self.driver.find_elements(MobileBy.XPATH, "//*[@text='联系人']/../following-sibling::*"))
        return self.search_count


    def to_member_info_page(self):
        """点击第一条搜索结果，进入到个人信息页"""
        self.first_ele.click()
        return MemberInfoPage(self.driver)